package com.dragon.mmochat.service;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.dao.DataIntegrityViolationException;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_19_10C;
import com.dragon.mmochat.message.Mmochat_19_11C;
import com.dragon.mmochat.message.Mmochat_19_12C;
import com.dragon.mmochat.message.Mmochat_19_13C;
import com.dragon.mmochat.message.Mmochat_19_14C;
import com.dragon.mmochat.message.Mmochat_19_15C;
import com.dragon.mmochat.message.Mmochat_19_16C;
import com.dragon.mmochat.message.Mmochat_19_17C;
import com.dragon.mmochat.message.Mmochat_19_18C;
import com.dragon.mmochat.message.Mmochat_19_19C;
import com.dragon.mmochat.message.Mmochat_19_1C;
import com.dragon.mmochat.message.Mmochat_19_1S;
import com.dragon.mmochat.message.Mmochat_19_20C;
import com.dragon.mmochat.message.Mmochat_19_21C;
import com.dragon.mmochat.message.Mmochat_19_22C;
import com.dragon.mmochat.message.Mmochat_19_23C;
import com.dragon.mmochat.message.Mmochat_19_24C;
import com.dragon.mmochat.message.Mmochat_19_25C;
import com.dragon.mmochat.message.Mmochat_19_26C;
import com.dragon.mmochat.message.Mmochat_19_2C;
import com.dragon.mmochat.message.Mmochat_19_2S;
import com.dragon.mmochat.message.Mmochat_19_3C;
import com.dragon.mmochat.message.Mmochat_19_4C;
import com.dragon.mmochat.message.Mmochat_19_5C;
import com.dragon.mmochat.message.Mmochat_19_6C;
import com.dragon.mmochat.message.Mmochat_19_6S;
import com.dragon.mmochat.message.Mmochat_19_7C;
import com.dragon.mmochat.message.Mmochat_19_8C;
import com.dragon.mmochat.message.Mmochat_19_9C;
import com.dragon.mmochat.message.Mmochat_5_10S;
import com.dragon.mmochat.message.Mmochat_5_14S;
import com.dragon.mmochat.message.Mmochat_5_1S;
import com.dragon.mmochat.model.enumType.MmochatDrugType;
import com.dragon.mmochat.model.enumType.MmochatEquipAttributeType;
import com.dragon.mmochat.model.enumType.MmochatMaterialType;
import com.dragon.mmochat.model.enumType.MmochatObjOwnerState;
import com.dragon.mmochat.model.enumType.MmochatObjectType;
import com.dragon.mmochat.model.enumType.MmochatPetStateType;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopState;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopType;
import com.dragon.mmochat.model.enumType.MmochatPropType;
import com.dragon.mmochat.model.enumType.MmochatRoleType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatSuperMachineType;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatEquipAttribute;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.shop.MmochatPlayerShop;
import com.dragon.mmochat.util.CreateTlvWin;
import com.dragon.mmochat.util.MmochatUtil;
import com.google.gson.reflect.TypeToken;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.SkymobiProtocolMessage;
import com.skymobi.message.widget.AddDataTLV;
import com.skymobi.message.widget.AddEditBoxTLV;
import com.skymobi.message.widget.ClearDataTLV;
import com.skymobi.message.widget.CloseWinTLV;
import com.skymobi.message.widget.CreateEditBoxTLV;
import com.skymobi.message.widget.CreateMsgBoxTLV;
import com.skymobi.message.widget.SendDataTLV;
import com.skymobi.message.widget.ShowPopupMenuTLV;
import com.skymobi.message.widget.ShowWaitingMsgBoxTLV;
import com.skymobi.message.widget.WinType;
import com.skymobi.service.SkymobiService;
import com.skymobi.util.Text;

//玩家商店系统
public class MmochatShopService {

	public static ScheduledExecutorService reloadShopThread = Executors
			.newScheduledThreadPool(1);

	// 商店：Map<商店类型, 商店列表>
	public static Map<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>> shops = new ConcurrentHashMap<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>>();

	// 商店：Map<商店ID,商店>;需要在shops更新时同步进行更新
	public static Map<Integer, MmochatPlayerShop> shopMap = new ConcurrentHashMap<Integer, MmochatPlayerShop>();

	// 商店星级图标:0级-10级
	private static final String[] shopStarString = new String[] { "-", "☆",
			"★", "★☆", "★★", "★★☆", "★★★", "★★★☆", "★★★★", "★★★★☆", "★★★★★" };

	// 商店各星级对应的基础税率:0级-10级
	private static final double[] shopStarBaseTax = new double[] { 0.1, 0.097,
			0.093, 0.089, 0.085, 0.08, 0.074, 0.067, 0.059, 0.05, 0.04 };

	// 商店升级积分要求:1级-10级
	private static final long[] shopLevelScore = new long[] { 100000, 500000,
			3000000, 10000000, 40000000, 200000000, 500000000, 1000000000,
			2000000000, 4000000000L };

	// 商店每级每天消耗的积分:0级-10级
	private static final int[] shopLevelDayCost = new int[] { 0, 5000, 10000,
			50000, 200000, 500000, 1000000, 2500000, 5000000, 8000000, 10000000 };

	// 商店购买记录:Map<买家ID，Map<商店ID, 消费额>>
	public static Map<Integer, Map<Integer, Long>> buyLog = new ConcurrentHashMap<Integer, Map<Integer, Long>>();

	public static boolean isShopReady = false;// 商店是否加载完成

	@SuppressWarnings("deprecation")
	public void init() {
		// 异步启动加载商店
		new Thread(new loadAllShop()).start();

		// 每天2:05进行商店排名、扣租金等事
		Date now = new Date();
		int hour = now.getHours();
		int minutes = now.getMinutes();
		int sec = now.getSeconds();
		int startTime = (int) ((60 - sec) + (65 - minutes - 1)
				* MmochatConstant.SECOND_PER_MIN + (2 - hour - 1)
				* MmochatConstant.SECOND_PER_HOUR);
		if (startTime <= 0) {
			startTime += MmochatConstant.SECOND_PER_DAY;
		}
		reloadShopThread.scheduleAtFixedRate(new RankShop(), startTime,
				MmochatConstant.SECOND_PER_DAY, TimeUnit.SECONDS);
	}

	// 从数据库加载所有商店
	class loadAllShop implements Runnable {
		public void run() {
			try {
				if (!MmochatMain.openMergeServer) {
					isShopReady = false;
					int lastShopId = MmochatDao.createNewShopId();// 新创建的商店ID
					System.out.println("开始加载商店");
					long t = System.currentTimeMillis();
					List<MmochatPlayerShop> shopList = new ArrayList<MmochatPlayerShop>();
					// 每次加载100个商店
					int step = 100;
					for (int i = 0; i < lastShopId; i += step) {
						List<MmochatPlayerShop> newList = null;
						boolean success = false;
						for (int s = 0; s < 20; s++) {
							try {
								newList = MmochatDao.getAllShops(i, i + step);
								success = true;
								try {
									Thread.sleep(50);
								} catch (Exception e1) {
								}
								break;
							} catch (Exception e) {
								System.out.println("第" + i + "批商店加载失败，第"
										+ (s + 1) + "次重新加载");
								int wait = 1000 + s * 500;
								try {
									Thread.sleep(wait);
								} catch (Exception e1) {
								}
							}
						}
						if (!success) {
							// 商店部分加载失败
							System.out.println("商店加载失败，请重启服务器!");
							return;
						}

						if (newList != null && newList.size() > 0) {
							shopList.addAll(newList);
							newList.clear();
						}
						System.out.println("已加载商店" + shopList.size() + "/"
								+ (lastShopId - 1));
					}
					t = System.currentTimeMillis() - t;
					System.out.println("加载商店完成，耗时" + t + "ms");
					if (shopList != null) {
						Collections.sort(shopList);
						Map<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>> shopBak = new ConcurrentHashMap<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>>();
						Map<Integer, MmochatPlayerShop> shopMapBak = new ConcurrentHashMap<Integer, MmochatPlayerShop>();
						for (MmochatPlayerShopType type : MmochatPlayerShopType
								.values()) {
							ConcurrentLinkedQueue<MmochatPlayerShop> typeShop = new ConcurrentLinkedQueue<MmochatPlayerShop>();
							shopBak.put(type, typeShop);
						}
						int rank = 1;
						for (MmochatPlayerShop shop : shopList) {
							// 兼容操作：将商店中的吸属白水晶改为黑水晶
							try {
								for (MmochatPackageGrid grid : shop.getGrids()
										.values()) {
									if (grid != null) {
										MmochatCommonObject obj = grid
												.getObject();
										if (obj != null) {
											if (obj.getPropType() != null) {
												if (obj.getPropType() == MmochatPropType.白水晶
														|| obj.getPropType() == MmochatPropType.超级白水晶) {
													if (obj
															.getEquipAttrInProp() != null) {
														// 吸属过了
														obj
																.setPropType(MmochatPropType.黑水晶);
														obj
																.setCanOverlap(MmochatPropType.黑水晶
																		.isCanOverLap());
													} else if (!obj
															.isCanOverlap()) {
														// 未吸属，不可叠加
														obj
																.setCanOverlap(obj
																		.getPropType()
																		.isCanOverLap());
													}
												}
											}
										}
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
							}

							shop.setRank(rank);
							rank++;
							ConcurrentLinkedQueue<MmochatPlayerShop> typeShop = shopBak
									.get(shop.getShopType());
							if (typeShop != null) {
								typeShop.add(shop);
							}
							shopMapBak.put(shop.getId(), shop);
						}
						Map<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>> oldShops = shops;
						shops = shopBak;
						Map<Integer, MmochatPlayerShop> oldShopMap = shopMap;
						shopMap = shopMapBak;

						// 释放旧的引用
						for (ConcurrentLinkedQueue<MmochatPlayerShop> queue : oldShops
								.values()) {
							queue.clear();
						}
						oldShops.clear();
						oldShops = null;
						oldShopMap.clear();
						oldShopMap = null;
					}
					isShopReady = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 2:05开始商店排序:2:00-2:30分期间不能创建新商店，方便商店排序
	class RankShop implements Runnable {
		public void run() {
			try {
				// 每个商店扣除租金、扣除每日积分消耗
				for (MmochatPlayerShop shop : shopMap.values()) {
					if (shop != null) {
						int cost = Math.max(1, getShopDailyCostMoney(shop));
						shop.addShopMoney(-1 * cost);
						int scoreCost = getShopDailyScoreCost(shop);
						shop.addScore(-1 * scoreCost);
						if (shop.getShopMoney() <= 0) {
							// 倒闭，发出提醒
							shop.setState(MmochatPlayerShopState.倒闭);
							shopMap.remove(shop.getId());
							MmochatGmService.sendKufuPrivateMsg(shop
									.getRoleId(), MmochatUtil.wrapColor("您的商店<"
									+ shop.getName() + ">经营不善，已经倒闭!店内物品被系统没收。",
									Color.red), false);
						} else if (shop.getShopMoney() <= 5 * cost) {
							// 不足5天，发出提醒
							int restDay = (int) Math.ceil(1.0
									* shop.getShopMoney() / cost);

							MmochatGmService.sendKufuPrivateMsg(shop
									.getRoleId(), MmochatUtil.wrapColor("您的商店<"
									+ shop.getName() + ">目前资金只够维持正常运营不足"
									+ restDay + "天，" + restDay
									+ "天后商店将倒闭,商店倒闭时将没收商店和店内所有物品。请尽快补充资金!",
									Color.red), false);
						}
						// 保存数据库
						try {
							MmochatDao.updateShopDailyPay(cost, shop.getState()
									.getKey(), scoreCost, shop.getId());
						} catch (Exception e) {
							e.printStackTrace();
						}
						Thread.sleep(10);
					}
				}

				// 按积分排序
				List<MmochatPlayerShop> shopList = new ArrayList<MmochatPlayerShop>(
						shopMap.values());
				if (shopList != null) {
					Collections.sort(shopList);
					Map<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>> shopBak = new ConcurrentHashMap<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>>();
					Map<Integer, MmochatPlayerShop> shopMapBak = new ConcurrentHashMap<Integer, MmochatPlayerShop>();
					for (MmochatPlayerShopType type : MmochatPlayerShopType
							.values()) {
						ConcurrentLinkedQueue<MmochatPlayerShop> typeShop = new ConcurrentLinkedQueue<MmochatPlayerShop>();
						shopBak.put(type, typeShop);
					}
					int rank = 1;
					for (MmochatPlayerShop shop : shopList) {
						shop.setRank(rank);
						rank++;
						ConcurrentLinkedQueue<MmochatPlayerShop> typeShop = shopBak
								.get(shop.getShopType());
						if (typeShop != null) {
							typeShop.add(shop);
						}
						shopMapBak.put(shop.getId(), shop);
					}
					Map<MmochatPlayerShopType, ConcurrentLinkedQueue<MmochatPlayerShop>> oldShops = shops;
					shops = shopBak;
					Map<Integer, MmochatPlayerShop> oldShopMap = shopMap;
					shopMap = shopMapBak;

					// 释放旧的引用
					for (ConcurrentLinkedQueue<MmochatPlayerShop> queue : oldShops
							.values()) {
						queue.clear();
					}
					oldShops.clear();
					oldShops = null;
					oldShopMap.clear();
					oldShopMap = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 获取我的商店列表
	public static CreateTlvWin getMyShopListTlv(MmochatPlayer me) {
		List<MmochatPlayerShop> myShopList = new ArrayList<MmochatPlayerShop>();
		for (MmochatPlayerShop shop : MmochatShopService.shopMap.values()) {
			if (shop.getRoleId() == me.getRoleId()
					&& shop.getState() != MmochatPlayerShopState.倒闭) {
				myShopList.add(shop);
			}
		}
		if (myShopList.size() == 0) {
			return null;
		}

		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("进入");
		tlv.setRightName("返回");
		tlv.setTitle("我的商店");
		tlv.setWinHeight(208);
		tlv.addRightTLV(new CloseWinTLV());

		for (MmochatPlayerShop shop : myShopList) {
			String item = MmochatUtil.wrapColor(shop.getName(), Color.yellow);
			item += "\n类型:" + shop.getShopType().toString();
			item += "\n信用:" + MmochatShopService.getShopStar(shop);
			item += "\n状态:" + shop.getState().toString();
			tlv.addItemEvent(item, new ClearDataTLV(), new AddDataTLV(
					(int) shop.getId()));
		}
		tlv.addLeftTLV(new AddDataTLV((int) 0));
		tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_6, (int) me.getRoleId()));
		tlv.addLeftTLV(new ShowWaitingMsgBoxTLV());

		return tlv;
	}

	// 查看商店功能列表
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_9(SkymobiHandler handler,
			Mmochat_19_9C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (!isShopReady) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会正在加载中，请过几分钟再来!");
		}

		switch (req.getIndex()) {
		case 1: {
			// 查看所有商店
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.List);
			tlv.setLeftName("查看");
			tlv.setRightName("返回");
			tlv.setTitle("所有商店");
			tlv.setWinHeight(208);
			tlv.addRightTLV(new CloseWinTLV());

			for (MmochatPlayerShopType shopType : MmochatPlayerShopType
					.values()) {
				tlv.addItemEvent(shopType.toString(), new ClearDataTLV(),
						new AddDataTLV((int) shopType.getKey()));
			}
			tlv.addLeftTLV(new AddDataTLV((int) 0));
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_19_1, (int) me
							.getRoleId()));
			tlv.addLeftTLV(new ShowWaitingMsgBoxTLV());

			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 2: {
			// 进入我的商店
			CreateTlvWin tlv = getMyShopListTlv(me);
			if (tlv == null) {
				return MmochatUtil.msgbox(req.getHallState(), "您还没有自己的商店!");
			}
			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 3: {
			// 创建商店
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.List);
			tlv.setLeftName("创建");
			tlv.setRightName("返回");
			tlv.setTitle("创建商店");
			tlv.setWinHeight(208);
			tlv.addRightTLV(new CloseWinTLV());

			for (MmochatPlayerShopType shopType : MmochatPlayerShopType
					.values()) {
				tlv.addItemEvent("创建" + shopType.toString(),
						new ClearDataTLV(), new AddDataTLV((int) shopType
								.getKey()));
			}
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_19_4, (int) me
							.getRoleId()));
			tlv.addLeftTLV(new ShowWaitingMsgBoxTLV());

			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 4: {
			// 购买二手商店
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.List);
			tlv.setLeftName("查看");
			tlv.setRightName("返回");
			tlv.setTitle("二手商店");
			tlv.setWinHeight(208);
			tlv.addRightTLV(new CloseWinTLV());

			for (MmochatPlayerShopType shopType : MmochatPlayerShopType
					.values()) {
				tlv.addItemEvent(shopType.toString(), new ClearDataTLV(),
						new AddDataTLV((int) shopType.getKey()));
			}
			tlv.addLeftTLV(new AddDataTLV((int) 0));
			tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
					MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_19_20, (int) me
							.getRoleId()));
			tlv.addLeftTLV(new ShowWaitingMsgBoxTLV());

			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 5: {
			// 掌柜手册
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.Text);
			tlv.setRightName("返回");
			tlv.setTitle("掌柜手册");
			tlv.setWinHeight(208);
			tlv.addRightTLV(new CloseWinTLV());
			String content = MmochatUtil.wrapColor("[如何开业]\n", Color.yellow);
			content += "创建商店后，商店就会自动开业，大家进入<我的商店>，就可以进行上货了。\n\n";

			content += MmochatUtil.wrapColor("[商店每日消耗]\n", Color.yellow);
			content += "每天凌晨2点系统会来收取租金，每个柜台每天要交"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.dailyMoneyCostPerShopPage)
					+ "文钱。租金是从<店内资金>里扣除的，"
					+ MmochatUtil.wrapColor("如果店内资金不够交租金时，商店会自动倒闭，店内物品会被系统没收。",
							Color.red)
					+ "这点各位亲们要特别注意噢，所以如果店内资金不足时，要记得补充资金。\n\n";

			content += MmochatUtil.wrapColor("[店内资金]\n", Color.yellow);
			content += "出售物品后获得的金钱会先存在店内资金里，商店每日消耗也会从店内资金里扣除。"
					+ "在<我的商店>菜单中，可以进行<资金管理>，存取资金。\n\n";

			content += MmochatUtil.wrapColor("[商店星级]\n", Color.yellow);
			content += "商店分" + shopStarString.length + "个级别，由低到高分别是";
			for (int i = 0; i < shopStarString.length; i++) {
				content += shopStarString[i] + "、";
			}
			content += "星级越高，商店排名越靠前，并且交的税率越低。\n\n";

			content += MmochatUtil.wrapColor("[星级晋升规则]\n", Color.yellow);
			content += "商店每出售一件物品，都会增加商店积分，积分与售价相同，"
					+ "当积分达到一定额度后，商店就会晋升到下一个星级。另外商店每天都会消耗一定的积分，"
					+ "没有星的商店不消耗积分，商店星级越高，每天消耗的积分越多。积分不足时，商店星级也会下降。\n\n";

			content += MmochatUtil.wrapColor("[商店税率]\n", Color.yellow);
			content += "税率是出售物品时要扣除的利润百分比，如税率为10%，出售1万文钱的物品，则商店获得9000文钱。"
					+ "商店税率由商店星级和柜台数决定。星级越高，税率越低。柜台数越高，税率越低。";

			tlv.setContent(new Text(content));

			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		case 6: {
			// 按名字找商品
			CreateTlvWin tlv = new CreateTlvWin();
			tlv.setWinType(WinType.List);
			tlv.setLeftName("搜索");
			tlv.setRightName("返回");
			tlv.setTitle("搜索指定商品");
			tlv.setWinHeight(208);
			tlv.addRightTLV(new CloseWinTLV());

			for (MmochatObjectType objType : MmochatObjectType.values()) {
				String hint;
				switch (objType) {
				case 道具:
					hint = "请输入完整准确的道具名!如：超级改造石";
					break;
				case 材料:
					hint = "请输入完整准确的材料名!如：植物精油";
					break;
				case 装备:
					hint = "请输入装备或首饰名。名字后跟上#属性，可指定特定属性，允许指定多条属性。示例：\n"
							+ MmochatUtil
									.wrapColor("乾坤珠\n乾坤珠#所有天赋\n乾坤珠#所有天赋#灵巧",
											Color.magenta);
					break;
				case 宠物:
					hint = "请输入宠物名。名字后跟上#等级，可查找等级范围的宠物。示例:\n"
							+ MmochatUtil.wrapColor("树妖\n树妖#85\n",
									Color.magenta) + "树妖#85:可查找75-85级的树妖";
					break;
				case 药品:
					hint = "请输入完整准确的药品名!如：活血草";
					break;
				default:
					continue;
				}
				CreateEditBoxTLV edit = new CreateEditBoxTLV();
				edit.setTitle(hint);
				edit.setLeftName("输入");
				edit.setRightName("取消");
				edit.setContentLenMax(100);
				edit.setBoxType(CreateEditBoxTLV.TYPE_MSGBOX);
				edit.setInputType(CreateEditBoxTLV.INPUT_ANY << 4);
				edit.addLeftTLV(new ClearDataTLV());
				edit.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
				edit.addLeftTLV(new AddDataTLV((int) objType.getValue()));
				edit.addLeftTLV(new AddDataTLV((int) 0)); // 页码
				edit.addLeftTLV(new AddEditBoxTLV());
				edit.addLeftTLV(new SendDataTLV(MmochatConstant
						.getClientModuleId(), MmochatMain.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_19_26, null));
				edit.addLeftTLV(new ShowWaitingMsgBoxTLV());

				tlv.addItemEvent(objType.toString(), new ClearDataTLV(), edit);
			}

			return MmochatUtil.tlvResponse(req.getHallState(), tlv);
		}
		default:
			break;
		}
		return null;
	}

	// 查看指定页，指定类型的商店列表
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_1(SkymobiHandler handler,
			Mmochat_19_1C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShopType type = MmochatPlayerShopType.get(req
				.getShopTypeValue());
		if (type == null) {
			return MmochatUtil.msgbox(req.getHallState(), "没有此类型的商店!");
		}
		ConcurrentLinkedQueue<MmochatPlayerShop> shopQueue = shops.get(type);
		if (shopQueue == null) {
			return MmochatUtil.msgbox(req.getHallState(), "暂时还没有此类商店开张!");
		}
		MmochatPlayerShop[] shopArray = shopQueue
				.toArray(new MmochatPlayerShop[0]);
		List<MmochatPlayerShop> shopList = new ArrayList<MmochatPlayerShop>();
		for (MmochatPlayerShop shop : shopArray) {
			if (shop != null && shop.getState() == MmochatPlayerShopState.营业中) {
				shopList.add(shop);
			}
		}
		Mmochat_19_1S pack = new Mmochat_19_1S();
		pack.setHallState(req.getHallState());
		pack.setType(type);
		pack.setCurPage(req.getPage());
		pack.setShopList(shopList);

		return pack;
	}

	// 查看指定ID、指定柜台的商店信息
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_2(SkymobiHandler handler,
			Mmochat_19_2C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopid());
		if (shop == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店已倒闭或不存在!");
		}
		if (shop.getState() != MmochatPlayerShopState.营业中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店目前暂未营业!");
		}
		Mmochat_19_2S pack = new Mmochat_19_2S();
		pack.setHallState(req.getHallState());
		pack.setShop(shop);
		pack.setCurPage(req.getPage());
		return pack;
	}

	// 查看指定ID、指定柜台的商店信息
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_3(SkymobiHandler handler,
			Mmochat_19_3C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopid());
		if (shop == null) {
			return MmochatUtil.msgbox(req.getHallState(), "当前商店不存在,请退回商店列表!");
		}
		ConcurrentLinkedQueue<MmochatPlayerShop> shopList = shops.get(shop
				.getShopType());
		if (shopList == null) {
			return MmochatUtil.msgbox(req.getHallState(), "当前商店不存在,请退回商店列表!");
		}
		MmochatPlayerShop targetShop = shop;
		if (req.getDirect() == 0) {
			// 查找前一个商店
			for (MmochatPlayerShop s : shopList) {
				if (s.getId() == shop.getId()) {
					// 当前商店已找到
					break;
				}
				if (s.getState() == MmochatPlayerShopState.营业中) {
					targetShop = s;
				}
			}
		} else {
			// 查找后一个商店
			boolean findCurShop = false;
			for (MmochatPlayerShop s : shopList) {
				if (s.getId() == shop.getId()) {
					// 当前商店已找到
					findCurShop = true;
				} else if (findCurShop) {
					if (s.getState() == MmochatPlayerShopState.营业中) {
						targetShop = s;
						break;
					}
				}
			}
		}

		Mmochat_19_2S pack = new Mmochat_19_2S();
		pack.setHallState(req.getHallState());
		pack.setShop(targetShop);
		pack.setCurPage(0);
		return pack;
	}

	// 创建商店时选择类型
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_4(SkymobiHandler handler,
			Mmochat_19_4C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		if (me.isGuest()) {
			return MmochatUtil.msgbox(req.getHallState(), "游客不能创建商店!");
		}

		// 等级达到30级，交纳10W文钱就可以创建一个商店，每个玩家最多创建商店数有限制
		if (me.getLevel() < MmochatConstant.levelNeedToCreateShop) {
			return MmochatUtil.msgbox(req.getHallState(), "人物达到"
					+ MmochatConstant.levelNeedToCreateShop + "级才能创建商店!");
		}
		// 已有商店数判断
		int curShopNum = 0;
		for (MmochatPlayerShop shop : MmochatShopService.shopMap.values()) {
			if (shop.getRoleId() == me.getRoleId()
					&& shop.getState() != MmochatPlayerShopState.倒闭) {
				curShopNum++;
				if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
					break;
				}
			}
		}
		if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经拥有"
					+ MmochatConstant.maxShopNumPerRole + "家商店了，已经够您忙的了!");
		}
		// TODO:其它创建条件

		MmochatPlayerShopType shopType = MmochatPlayerShopType.get(req
				.getShopTypeValue());
		if (shopType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "商店类型不存在!");
		}
		// 创建
		CreateEditBoxTLV edit = new CreateEditBoxTLV();
		edit.setTitle("创建" + shopType.toString() + "需要金钱"
				+ MmochatUtil.getColorMoney(MmochatConstant.moneyForCreatShop)
				+ "文,请输入要创建的商店名,1-8字!");
		edit.setLeftName("创建");
		edit.setRightName("取消");
		edit.setContentLenMax(8);
		edit.setBoxType(CreateEditBoxTLV.TYPE_MSGBOX);
		edit.setInputType(CreateEditBoxTLV.INPUT_ANY << 4);
		edit.addLeftTLV(new ClearDataTLV());
		edit.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
		edit.addLeftTLV(new AddDataTLV((int) req.getShopTypeValue()));
		edit.addLeftTLV(new AddEditBoxTLV());
		edit.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_5, null));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		edit.addLeftTLV(wait);

		return MmochatUtil.tlvResponse(req.getHallState(), edit);
	}

	// 创建商店
	@SuppressWarnings("deprecation")
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_5(SkymobiHandler handler,
			Mmochat_19_5C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		// 2:00-2:30分期间不能创建新商店，方便商店排序
		Date now = new Date();
		if (now.getHours() == 2 && now.getMinutes() < 30) {
			return MmochatUtil.msgbox(req.getHallState(),
					"每天凌晨2:00-2:30期间不能创建新的商店，敬请见谅!");
		}

		// 等级达到30级，交纳10W文钱就可以创建一个商店，每个玩家最多创建商店数有限制
		if (me.getLevel() < MmochatConstant.levelNeedToCreateShop) {
			return MmochatUtil.msgbox(req.getHallState(), "人物达到"
					+ MmochatConstant.levelNeedToCreateShop + "级才能创建商店!");
		}
		// 金钱判断
		if (me.getSmall_money() < MmochatConstant.moneyForCreatShop) {
			return MmochatUtil.msgbox(req.getHallState(), "创建商店需要"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.moneyForCreatShop)
					+ "文,您的金钱不够!");
		}

		// 商店类型
		MmochatPlayerShopType shopType = MmochatPlayerShopType.get(req
				.getShopTypeValue());
		if (shopType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "商店类型不存在!");
		}

		// 已有商店数判断
		int curShopNum = 0;
		for (MmochatPlayerShop shop : MmochatShopService.shopMap.values()) {
			if (shop.getRoleId() == me.getRoleId()
					&& shop.getState() != MmochatPlayerShopState.倒闭) {
				curShopNum++;
				if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
					break;
				}
			}
		}
		if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经拥有"
					+ MmochatConstant.maxShopNumPerRole + "家商店了，已经够您忙的了!");
		}
		// 商店名检查
		String shopName = req.getName();
		if (shopName == null || shopName.length() < 1 || shopName.length() > 8) {
			return MmochatUtil.msgbox(req.getHallState(), "商店名必需是1到8个字!请重新输入。");
		}

		// 非法字检测
		if (!MmochatMainService.isRoleNameValid(shopName)) {
			return MmochatUtil.msgbox(req.getHallState(), "商店名中包含非法字词!请重新输入。");
		}

		// 扣除创建商店的资金
		synchronized (me) {
			if (me.getSmall_money() < MmochatConstant.moneyForCreatShop) {
				return MmochatUtil
						.msgbox(
								req.getHallState(),
								"创建商店需要"
										+ MmochatUtil
												.getColorMoney(MmochatConstant.moneyForCreatShop)
										+ "文,您的金钱不够!");
			}
			me.addAndUpdateSmall_money(-1 * MmochatConstant.moneyForCreatShop);
		}

		// 创建商店
		MmochatPlayerShop shop = new MmochatPlayerShop();
		shop.setRoleId(me.getRoleId());
		shop.setRoleName(me.getName());
		shop.setShopType(shopType);
		shop.setName(shopName);
		shop.initGrids();
		shop.setState(MmochatPlayerShopState.营业中);
		shop.setShopMoney((long) (0.3 * MmochatConstant.moneyForCreatShop));
		try {
			MmochatDao.createNewShop(shop, MmochatConstant.moneyForCreatShop,
					me.getRoleId());
		} catch (DataIntegrityViolationException e) {
			me.addAndUpdateSmall_money(MmochatConstant.moneyForCreatShop);
			return MmochatUtil.msgbox(req.getHallState(), "该商店名已存在!");
		} catch (Exception e) {
			e.printStackTrace();
			me.addAndUpdateSmall_money(MmochatConstant.moneyForCreatShop);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatStatisticsService.addSmallMoneyCost(
				MmochatSmallMoneyCostType.创建商店, -1
						* MmochatConstant.moneyForCreatShop);
		shopMap.put(shop.getId(), shop);
		shop.setRank(shopMap.size());
		ConcurrentLinkedQueue<MmochatPlayerShop> shopList = shops.get(shopType);
		shopList.add(shop);

		String systemMsg = MmochatUtil.wrapColor(me.getName(), Color.green)
				+ "创建了一家新的" + shopType.toString() + "<"
				+ MmochatUtil.wrapColor(shopName, Color.yellow) + ">,欢迎大家光临!";
		MmochatChatService.sendSystemMsg(systemMsg);
		return MmochatUtil.msgbox(req.getHallState(), "创建成功!点击<进入我的商店>即可开始管理，"
				+ "强烈推荐熟读<" + MmochatUtil.wrapColor("掌柜手册", Color.yellow)
				+ ">，祝您生意兴隆!");
	}

	// 查看我的商店
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_6(SkymobiHandler handler,
			Mmochat_19_6C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(req.getPage());
		pack.setShop(shop);
		return pack;
	}

	// 物品上架
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_7(SkymobiHandler handler,
			Mmochat_19_7C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店正在出售中!");
		}
		MmochatPackageGrid shopGrid = shop.getGrids().get(req.getShopGridId());
		if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
			// 此格子不能使用，那么另选格子
			shopGrid = shop.getFirstSpaceGrid();
		}
		if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
			return MmochatUtil.msgbox(req.getHallState(), "商店货物已满!");
		}
		MmochatPackageGrid objGrid = me.getMyPackage().get(
				req.getPackageGridId());
		if (objGrid == null || !objGrid.isCanUse() || !objGrid.hasObj()) {
			return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
		}
		MmochatCommonObject obj = objGrid.getObject();
		if (obj == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
		}
		if (!obj.isCanDeal()) {
			return MmochatUtil.msgbox(req.getHallState(), "此物品无法出售!");
		}
		if (obj.isUsedOut()) {
			return MmochatUtil.msgbox(req.getHallState(), "此物品已用尽!");
		}
		if (obj.getBorrowFromRoleId() != null) {
			return MmochatUtil.msgbox(req.getHallState(), "此物品是借来的，无法出售!");
		}
		// 神行法宝不能出售，因为体力消耗的问题
		if (obj.getSuperMachineType() != null
				&& obj.getSuperMachineType() == MmochatSuperMachineType.神行法宝) {
			return MmochatUtil.msgbox(req.getHallState(), "神行法宝不能在商店出售!");
		}

		int num = Math.min(obj.getOverlapNum(), req.getNum());
		if (num <= 0 || num > MmochatConstant.maxNumToOverlapOfPackage) {
			return MmochatUtil.msgbox(req.getHallState(), "出售数量必须大于0且不超过"
					+ MmochatConstant.maxNumToOverlapOfPackage + "!");
		}
		if (req.getPrice() <= 0 || req.getPrice() > (long) Integer.MAX_VALUE) {
			return MmochatUtil.msgbox(req.getHallState(), "出售价格必须大于0且小于21亿!");
		}
		int page = shop.getPageByGrid(shopGrid);

		// 一个商店有一个version，对商店进行任何改变商店格子数据的操作时，锁住商店，对version进行判断是否一致，
		// 不一致则退出，一致时version++，并进行内存和数据库操作，完成时再解锁
		if (shop.getVersion() != req.getShopVersion()) {
			// 更新商店
			Mmochat_19_6S pack = new Mmochat_19_6S();
			pack.setHallState(req.getHallState());
			pack.setCurPage(page);
			pack.setShop(shop);
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "您的商店当前正在售物，请稍后再试!");
		}

		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			shopGrid = shop.getGrids().get(req.getShopGridId());
			if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
				// 此格子不能使用，那么另选格子
				shopGrid = shop.getFirstSpaceGrid();
			}
			if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
				return MmochatUtil.msgbox(req.getHallState(), "商店货物已满!");
			}
			objGrid = me.getMyPackage().get(req.getPackageGridId());
			if (objGrid == null || !objGrid.isCanUse() || !objGrid.hasObj()) {
				return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
			}
			obj = objGrid.getObject();
			if (obj == null) {
				return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
			}
			if (!obj.isCanDeal()) {
				return MmochatUtil.msgbox(req.getHallState(), "此物品无法出售!");
			}
			if (obj.isUsedOut()) {
				return MmochatUtil.msgbox(req.getHallState(), "此物品已用尽!");
			}
			num = Math.min(obj.getOverlapNum(), req.getNum());
			if (num <= 0) {
				return MmochatUtil.msgbox(req.getHallState(), "出售数量必须大于0!");
			}

			if (shop.getVersion() != req.getShopVersion()) {
				// 更新商店
				Mmochat_19_6S pack = new Mmochat_19_6S();
				pack.setHallState(req.getHallState());
				pack.setCurPage(page);
				pack.setShop(shop);
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil.msgbox(req.getHallState(),
						"您的商店当前正在售物，请稍后再试!");
			}

			MmochatCommonObject newObj = obj.getCloneWithNewId();
			newObj.setOverlapNum(num);
			newObj.setSellPrice(req.getPrice());

			// 将物品加入商店
			shopGrid.setObject(newObj);

			// 从包裹中去除
			int oldNum = obj.getOverlapNum();
			obj.delOverlapNum(num);
			if (obj.getOverlapNum() <= 0) {
				objGrid.setObject(null);
			}

			// 更新数据库
			try {
				MmochatDao.addObjToShop(me, shop);
			} catch (Exception e) {
				e.printStackTrace();
				// 恢复商店和包裹格子
				shopGrid.setObject(null);
				obj.setOverlapNum(oldNum);
				objGrid.setObject(obj);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}

			// 提升版本号应该放在所有操作完成之后
			shop.addVersion();
		}

		// 更新商店
		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(page);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		// 下发更新包裹数据
		Mmochat_5_1S packagePack = new Mmochat_5_1S();
		packagePack.addGrid(objGrid);
		MmochatUtil.sendCommonPack(me, packagePack);

		return null;
	}

	// 物品下架
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_8(SkymobiHandler handler,
			Mmochat_19_8C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店正在出售中!");
		}
		MmochatPackageGrid shopGrid = shop.getGrids().get(req.getShopGridId());
		if (shopGrid == null || !shopGrid.isCanUse()) {
			// 此格子不能使用，或没有货物
			Mmochat_19_6S pack = new Mmochat_19_6S();
			pack.setHallState(req.getHallState());
			pack.setCurPage(0);
			pack.setShop(shop);
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "此商品已经售出!");
		}
		int page = shop.getPageByGrid(shopGrid);

		MmochatCommonObject shopObj = shopGrid.getObject();
		if (shopObj == null) {
			Mmochat_19_6S pack = new Mmochat_19_6S();
			pack.setHallState(req.getHallState());
			pack.setCurPage(page);
			pack.setShop(shop);
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "此商品已经售出!");
		}

		if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
			if (me.isPetFull()) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您携带的宠物数量已满，请先存到仓库后再下货!");
			}
		} else {
			if (!me.hasEnoughPackageSpace(1)) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您的包裹已满，请先清理出空间再下货!");
			}
		}

		// 一个商店有一个version，对商店进行任何改变商店格子数据的操作时，锁住商店，对version进行判断是否一致，
		// 不一致则退出，一致时version++，并进行内存和数据库操作，完成时再解锁
		if (shop.getVersion() != req.getShopVersion()) {
			// 更新商店
			Mmochat_19_6S pack = new Mmochat_19_6S();
			pack.setHallState(req.getHallState());
			pack.setCurPage(page);
			pack.setShop(shop);
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "您的商店当前正忙，请稍后重试!");
		}

		MmochatPet pet = null;
		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			shopGrid = shop.getGrids().get(req.getShopGridId());
			if (shopGrid == null || !shopGrid.isCanUse()) {
				// 此格子不能使用，或没有货物
				return MmochatUtil.msgbox(req.getHallState(), "此商品已经售出!");
			}
			shopObj = shopGrid.getObject();
			if (shopObj == null) {
				return MmochatUtil.msgbox(req.getHallState(), "此商品已经售出!");
			}

			if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
				if (me.isPetFull()) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您携带的宠物数量已满，请先存到仓库后再下货!");
				}
			} else {
				if (!me.hasEnoughPackageSpace(1)) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您的包裹已满，请先清理出空间再下货!");
				}
			}

			if (shop.getVersion() != req.getShopVersion()) {
				// 更新商店
				Mmochat_19_6S pack = new Mmochat_19_6S();
				pack.setHallState(req.getHallState());
				pack.setCurPage(page);
				pack.setShop(shop);
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil
						.msgbox(req.getHallState(), "您的商店当前正忙，请稍后重试!");
			}
			// 将物品从商店中去除，并加到玩家包裹
			if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
				pet = shopObj.getPet();
				if (pet == null) {
					return MmochatUtil.msgbox(req.getHallState(), "此宠物已售出!");
				}
				boolean ret = me.addPet(pet);
				if (!ret) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您携带的宠物数量已满，请先存到仓库后再下货!");
				}
			} else {
				boolean ret = me.addObjectToPackage(shopObj);
				if (!ret) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您的包裹已满，请先清理出空间再下货!");
				}
			}
			shopGrid.setObject(null);

			// 更新数据库
			try {
				MmochatDao.getObjFromShop(me, shop);
			} catch (Exception e) {
				e.printStackTrace();
				// 恢复商店和包裹格子
				shopGrid.setObject(shopObj);
				if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
					if (pet != null) {
						me.delPet(pet.getId());
					}
				} else {
					me.delPackageObjById(shopObj.getId());
				}
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			shopObj.setSellPrice(0);

			// 提升版本号应该放在所有操作完成之后
			shop.addVersion();
		}

		// 更新商店
		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(page);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		if (pet != null) {
			Mmochat_5_10S petPack = new Mmochat_5_10S();
			petPack.addPet(pet);
			MmochatUtil.sendCommonPack(me, petPack);
		}
		return null;
	}

	// 宠物上架
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_24(SkymobiHandler handler,
			Mmochat_19_24C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店正在出售中!");
		}
		MmochatPackageGrid shopGrid = shop.getGrids().get(req.getGridId());
		if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
			// 此格子不能使用，那么另选格子
			shopGrid = shop.getFirstSpaceGrid();
		}
		if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
			return MmochatUtil.msgbox(req.getHallState(), "商店货物已满!");
		}
		MmochatPet pet = me.getPetAlongById(req.getPetId());
		if (pet == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此宠物不存在!");
		}
		if (pet.getOwnerState() != MmochatObjOwnerState.未认主) {
			return MmochatUtil
					.msgbox(req.getHallState(), "此宠物尚在<认主状态>，无法进行出售!");
		}
		if (pet.getBorrowFromRoleId() != null) {
			return MmochatUtil.msgbox(req.getHallState(), "此宠物是借来的，无法进行出售!");
		}
		if (req.getPrice() <= 0 || req.getPrice() > (long) Integer.MAX_VALUE) {
			return MmochatUtil.msgbox(req.getHallState(), "出售价格必须大于0且小于21亿!");
		}

		int page = shop.getPageByGrid(shopGrid);

		// 一个商店有一个version，对商店进行任何改变商店格子数据的操作时，锁住商店，对version进行判断是否一致，
		// 不一致则退出，一致时version++，并进行内存和数据库操作，完成时再解锁
		if (shop.getVersion() != req.getShopVersion()) {
			// 更新商店
			Mmochat_19_6S pack = new Mmochat_19_6S();
			pack.setHallState(req.getHallState());
			pack.setCurPage(page);
			pack.setShop(shop);
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "您的商店当前正在售物，请稍后再试!");
		}

		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			shopGrid = shop.getGrids().get(req.getGridId());
			if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
				// 此格子不能使用，那么另选格子
				shopGrid = shop.getFirstSpaceGrid();
			}
			if (shopGrid == null || !shopGrid.isCanUse() || shopGrid.hasObj()) {
				return MmochatUtil.msgbox(req.getHallState(), "商店货物已满!");
			}
			pet = me.getPetAlongById(req.getPetId());
			if (pet == null) {
				return MmochatUtil.msgbox(req.getHallState(), "此宠物不存在!");
			}

			if (shop.getVersion() != req.getShopVersion()) {
				// 更新商店
				Mmochat_19_6S pack = new Mmochat_19_6S();
				pack.setHallState(req.getHallState());
				pack.setCurPage(page);
				pack.setShop(shop);
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil.msgbox(req.getHallState(),
						"您的商店当前正在售物，请稍后再试!");
			}

			MmochatCommonObject obj = new MmochatCommonObject();
			obj.setObjectType(MmochatObjectType.宠物);
			obj.setPet(pet);
			obj.setSellPrice(req.getPrice());

			// 将宠物加入商店
			shopGrid.setObject(obj);

			// 从包裹中去除
			me.delPet(pet.getId());

			// 更新数据库
			try {
				MmochatDao.addPetToShop(me, shop);
			} catch (Exception e) {
				e.printStackTrace();
				// 恢复商店和宠物
				shopGrid.setObject(null);
				me.addPet(pet);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			pet.setState(MmochatPetStateType.休息);

			// 提升版本号应该放在所有操作完成之后
			shop.addVersion();
		}

		// 更新商店
		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(page);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		// 下发更新宠物数据
		Mmochat_5_14S petPack = new Mmochat_5_14S();
		petPack.setDelPetId(pet.getId());
		MmochatUtil.sendCommonPack(me, petPack);

		return null;
	}

	// 19.10 开业/停业
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_10(SkymobiHandler handler,
			Mmochat_19_10C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		MmochatPlayerShopState oldState = shop.getState();
		if (req.getIndex() == 0) {
			// 停业
			if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店正在出售中!");
			}
			if (shop.getState() == MmochatPlayerShopState.倒闭) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店已倒闭!");
			}

			if (shop.getState() == MmochatPlayerShopState.未营业) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店未营业!");
			}
			synchronized (shop) {
				if (!shop.isShopOwner(me.getRoleId())) {
					return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
				}
				if (shop.getState() != MmochatPlayerShopState.营业中) {
					return MmochatUtil.msgbox(req.getHallState(), "此商店未营业!");
				}
				shop.setState(MmochatPlayerShopState.未营业);
			}
		} else if (req.getIndex() == 1) {
			// 开业
			if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店正在出售中!");
			}
			if (shop.getState() == MmochatPlayerShopState.倒闭) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店已倒闭!");
			}

			if (shop.getState() == MmochatPlayerShopState.营业中) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店已经在营业中!");
			}
			if (shop.getShopMoney() <= 0) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此商店的营业资金为0，无法开业!");
			}
			synchronized (shop) {
				if (!shop.isShopOwner(me.getRoleId())) {
					return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
				}
				if (shop.getState() != MmochatPlayerShopState.未营业) {
					return MmochatUtil.msgbox(req.getHallState(), "此商店已经在营业中!");
				}
				shop.setState(MmochatPlayerShopState.营业中);
			}
		}
		try {
			MmochatDao.editShopState(shop.getState().getKey(), shop.getId());
		} catch (Exception e) {
			e.printStackTrace();
			shop.setState(oldState);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		return pack;
	}

	// 19.11 取出资金
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_11(SkymobiHandler handler,
			Mmochat_19_11C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		int money = req.getMoney();

		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			money = (int) Math.min(money, shop.getShopMoney());
			if (money <= 0) {
				return MmochatUtil.msgbox(req.getHallState(), "您的商店中资金为0!");
			}
			// 资金底限，超过部分才能取出
			int baseMoney = MmochatConstant.dailyMoneyCostPerShopPage
					* shop.getPageNum() * 5;
			long maxGetMoney = shop.getShopMoney() - baseMoney;
			if ((long) money > maxGetMoney) {
				return MmochatUtil
						.msgbox(
								req.getHallState(),
								"商店必需保留"
										+ MmochatUtil.getColorMoney(baseMoney)
										+ "文钱(柜台数X"
										+ MmochatUtil
												.getColorMoney(MmochatConstant.dailyMoneyCostPerShopPage)
										+ "X5)文钱作为五天的运营资金，您最多可取出"
										+ MmochatUtil
												.getColorMoney(maxGetMoney)
										+ "文钱!");
			}
			if ((long) Integer.MAX_VALUE - me.getSmall_money() < money) {
				// 取出后，包裹金钱超过上限
				return MmochatUtil.msgbox(req.getHallState(),
						"取出后，您包裹中的金钱会超过21亿上限，请先转移部分金钱再试!");
			}
			// 取钱与出售操作的并发
			try {
				MmochatDao.addShopMoney(me.getRoleId(), -1 * money, shop
						.getId());
			} catch (Exception e) {
				e.printStackTrace();
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			me.addAndUpdateSmall_money(money);
			shop.addShopMoney(-1 * money);
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "您从商店中取出了"
				+ MmochatUtil.getColorMoney(money) + "文钱!金钱已放入包裹!");
	}

	// 19.12 增加资金
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_12(SkymobiHandler handler,
			Mmochat_19_12C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		int money = req.getMoney();
		if (money <= 0) {
			return MmochatUtil.msgbox(req.getHallState(), "请输入大于0的数字!");
		}
		if (money > me.getSmall_money()) {
			return MmochatUtil.msgbox(req.getHallState(), "您包裹中的金钱不足!");
		}

		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			// 加钱与出售操作的并发
			try {
				MmochatDao.addShopMoney(me.getRoleId(), money, shop.getId());
			} catch (Exception e) {
				e.printStackTrace();
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			me.addAndUpdateSmall_money(-1 * money);
			shop.addShopMoney(money);
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		return pack;
	}

	// 获取商店公告TLV
	private CreateTlvWin getShopIntroTlv(MmochatPlayerShop shop, int roleId) {
		if (shop == null) {
			return null;
		}
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.Text);
		tlv.setLeftName("修改");
		tlv.setRightName("返回");
		tlv.setTitle("掌柜公告");
		tlv.setWinHeight(208);
		tlv.addRightTLV(new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_INTRO));
		tlv.setWinId(MmochatConstant.TLV_WIN_PLAYERSHOP_INTRO);

		String content = shop.getIntro();
		if (shop.getIntro() == null) {
			content = "暂无公告!";
		} else {
			content = MmochatFilterService.getFilterString(content);
		}
		tlv.setContent(new Text(content));

		CreateEditBoxTLV edit = new CreateEditBoxTLV();
		edit.setTitle("请输入公告");
		edit.setLeftName("确定");
		edit.setRightName("取消");
		edit.setContentLenMax(29);
		edit.setBoxType(CreateEditBoxTLV.TYPE_LOCAL);
		edit.setInputType(CreateEditBoxTLV.INPUT_ANY << 4);
		edit.addLeftTLV(new ClearDataTLV());
		edit.addLeftTLV(new AddDataTLV((int) roleId));
		edit.addLeftTLV(new AddDataTLV((int) shop.getId()));
		edit.addLeftTLV(new AddEditBoxTLV());
		edit.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_14, null));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		edit.addLeftTLV(wait);

		tlv.addLeftTLV(edit);
		return tlv;
	}

	// 19.13 查看公告
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_13(SkymobiHandler handler,
			Mmochat_19_13C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		CreateTlvWin tlv = getShopIntroTlv(shop, me.getRoleId());
		if (tlv == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		return MmochatUtil.tlvResponse(req.getHallState(), tlv);
	}

	// 19.14 修改公告
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_14(SkymobiHandler handler,
			Mmochat_19_14C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}

		String content = req.getIntro();
		if (content == null) {
			content = "";
		}

		if (content.contains("") || content.contains("")
				|| content.contains("") || content.contains("")) {
			return MmochatUtil.msgbox(req.getHallState(), "您的公告内容中包含不可见字符!");
		}

		content = MmochatFilterService.getFilterString(content);

		try {
			MmochatDao.editShopIntro(content, req.getShopId());
		} catch (Exception e) {
			e.printStackTrace();
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		shop.setIntro(content);

		CreateTlvWin tlv = getShopIntroTlv(shop, me.getRoleId());
		if (tlv == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_INTRO), tlv);
	}

	// 19.15 增减柜台
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_15(SkymobiHandler handler,
			Mmochat_19_15C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil
					.msgbox(req.getHallState(), "本店正在二手店出售中，无法进行此操作!");
		}
		if (shop.getState() != MmochatPlayerShopState.未营业) {
			return MmochatUtil.msgbox(req.getHallState(), "请先停止营业，再进行柜台增减操作!");
		}
		if (req.getAction() == 0) {
			// 增加柜台
			if (shop.getPageNum() >= MmochatConstant.maxPageNumPerShop) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此商店的柜台数已达上限，无法再增加!");
			}
			String msg = "增加一个柜台需要花费"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.moneyForAddShopPage)
					+ "文钱，并且每日营业成本消耗会增加"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.dailyMoneyCostPerShopPage)
					+ "文钱，您确定要增加么?";
			CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
			msgbox.setContent(new Text(msg));
			msgbox.setLeftName("确定");
			msgbox.setRightName("取消");
			msgbox.addLeftTLV(new ClearDataTLV());
			msgbox.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
			msgbox
					.addLeftTLV(new SendDataTLV(MmochatConstant
							.getClientModuleId(),
							MmochatMain.getModuleInstID(),
							MmochatConstant.MSGCODE_MMOCHAT_C_19_16, (int) shop
									.getId()));
			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			msgbox.addLeftTLV(wait);
			return MmochatUtil.tlvResponse(req.getHallState(), msgbox);
		} else if (req.getAction() == 1) {
			// 减少柜台
			if (shop.getPageNum() <= 1) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此商店的柜台数已达下限，无法再减少!");
			}
			if (req.getCurPage() != shop.getPageNum() - 1) {
				return MmochatUtil.msgbox(req.getHallState(), "必需先删除最后一个柜台!");
			}
			if (!shop.isPageClear(req.getCurPage())) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此柜台尚有物品在出售，请先清空此柜台!");
			}
			String msg = "您确定要删除此柜台么?";
			CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
			msgbox.setContent(new Text(msg));
			msgbox.setLeftName("确定");
			msgbox.setRightName("取消");
			msgbox.addLeftTLV(new ClearDataTLV());
			msgbox.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
			msgbox.addLeftTLV(new AddDataTLV((int) shop.getId()));
			msgbox.addLeftTLV(new SendDataTLV(MmochatConstant
					.getClientModuleId(), MmochatMain.getModuleInstID(),
					MmochatConstant.MSGCODE_MMOCHAT_C_19_17, (int) req
							.getCurPage()));
			ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
			wait.setShowCancel(false);
			msgbox.addLeftTLV(wait);
			return MmochatUtil.tlvResponse(req.getHallState(), msgbox);
		}
		return null;
	}

	// 19.16 增加柜台
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_16(SkymobiHandler handler,
			Mmochat_19_16C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil
					.msgbox(req.getHallState(), "本店正在二手店出售中，无法进行此操作!");
		}
		if (shop.getState() != MmochatPlayerShopState.未营业) {
			return MmochatUtil.msgbox(req.getHallState(), "请先停止营业，再进行柜台增减操作!");
		}
		if (shop.getPageNum() >= MmochatConstant.maxPageNumPerShop) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店的柜台数已达上限，无法再增加!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		if (me.getSmall_money() < MmochatConstant.moneyForAddShopPage) {
			return MmochatUtil.msgbox(req.getHallState(), "您包裹中没有"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.moneyForAddShopPage)
					+ "文钱!");
		}
		// 增加一页
		boolean ret = shop.addLastPage();
		if (!ret) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店的柜台数已达上限，无法再增加!");
		}
		try {
			MmochatDao.addShopPage(me.getRoleId(),
					MmochatConstant.moneyForAddShopPage, shop);
		} catch (Exception e) {
			e.printStackTrace();
			shop.delLastPage();
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		me.addAndUpdateSmall_money(-1 * MmochatConstant.moneyForAddShopPage);

		int cost = Math.max(1, getShopDailyCostMoney(shop));
		if (shop.getShopMoney() <= 5 * cost) {
			// 不足5天，发出提醒
			int restDay = (int) Math.ceil(1.0 * shop.getShopMoney() / cost);
			MmochatGmService.sendKufuPrivateMsg(shop.getRoleId(), MmochatUtil
					.wrapColor("您的商店<" + shop.getName() + ">目前资金只够维持正常运营不足"
							+ restDay + "天，" + restDay
							+ "天后商店将倒闭,商店倒闭时将没收商店和店内所有物品。请尽快补充资金!", Color.red),
					true);
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(shop.getPageNum() - 1);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "柜台已增加至"
				+ shop.getPageNum() + "个!");
	}

	// 19.17 减少柜台
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_17(SkymobiHandler handler,
			Mmochat_19_17C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() != MmochatPlayerShopState.未营业) {
			return MmochatUtil.msgbox(req.getHallState(), "请先停止营业，再进行柜台增减操作!");
		}
		if (shop.getPageNum() <= 1) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店的柜台数已达下限，无法再减少!");
		}
		if (req.getPage() != shop.getPageNum() - 1) {
			return MmochatUtil.msgbox(req.getHallState(), "必需先删除最后一个柜台!");
		}
		if (!shop.isPageClear(req.getPage())) {
			return MmochatUtil
					.msgbox(req.getHallState(), "此柜台尚有物品在出售，请先清空此柜台!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		boolean ret = shop.delLastPage();
		if (!ret) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店的柜台数已达下限，无法再减少!");
		}
		try {
			MmochatDao.updateShopPage(shop);
		} catch (Exception e) {
			e.printStackTrace();
			shop.addLastPage();
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "柜台已删除!");
	}

	// 19.18出售本店
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_18(SkymobiHandler handler,
			Mmochat_19_18C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (!MmochatConstant.isShopCanSell) {
			return MmochatUtil.msgbox(req.getHallState(), "商店转让功能维护中，暂不支持转让!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() == MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店已经在出售中!");
		}
		if (shop.getState() != MmochatPlayerShopState.未营业) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店必须先停止营业，才能出售!");
		}
		if (!shop.isAllPageClear()) {
			return MmochatUtil.msgbox(req.getHallState(),
					"此商店中还有物品在出售，必须全部下架后才能售卖本店!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		int price = req.getPrice();
		if (price <= 0 || price > 1000000000) {
			return MmochatUtil.msgbox(req.getHallState(), "商店出售价格错误!");
		}
		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			if (shop.getState() != MmochatPlayerShopState.未营业) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此商店必须先停止营业，才能出售!");
			}
			shop.setSell_price(price);
			shop.setState(MmochatPlayerShopState.二手店出售中);
			shop.addVersion();
		}
		try {
			MmochatDao.sellShop(shop.getState().getKey(), price, shop.getId());
		} catch (Exception e) {
			e.printStackTrace();
			shop.setState(MmochatPlayerShopState.未营业);
			shop.setSell_price(0);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "本店以"
				+ MmochatUtil.getColorMoney(price)
				+ "的价格正在出售中，您可以到<购买二手商店>中查看到!"
				+ "成功售出后，金钱会直接转到您的包裹中，本店中的剩余营业资金将一并移交。");
	}

	// 19.19取消出售本店
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_19(SkymobiHandler handler,
			Mmochat_19_19C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		if (shop.getState() != MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店已经不在出售中!");
		}
		synchronized (shop) {
			if (!shop.isShopOwner(me.getRoleId())) {
				return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
			}
			if (shop.getState() != MmochatPlayerShopState.二手店出售中) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店已经不在出售中!");
			}
			shop.setState(MmochatPlayerShopState.未营业);
			shop.addVersion();
		}
		try {
			MmochatDao.sellShop(shop.getState().getKey(), 0, shop.getId());
		} catch (Exception e) {
			e.printStackTrace();
			shop.setState(MmochatPlayerShopState.二手店出售中);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		shop.setSell_price(0);

		Mmochat_19_6S pack = new Mmochat_19_6S();
		pack.setHallState(req.getHallState());
		pack.setCurPage(0);
		pack.setShop(shop);
		MmochatUtil.sendCommonPack(me, pack);

		return MmochatUtil.msgbox(req.getHallState(), "本店已经取消出售。");
	}

	// 获取二手店的tlv页面
	private CreateTlvWin getSellShopTlv(MmochatPlayerShopType type, int page,
			int roleId) {
		ConcurrentLinkedQueue<MmochatPlayerShop> shopQueue = shops.get(type);
		if (shopQueue == null) {
			return null;
		}
		MmochatPlayerShop[] shopArray = shopQueue
				.toArray(new MmochatPlayerShop[0]);
		List<MmochatPlayerShop> shopList = new ArrayList<MmochatPlayerShop>();
		for (MmochatPlayerShop shop : shopArray) {
			if (shop != null
					&& shop.getState() == MmochatPlayerShopState.二手店出售中) {
				shopList.add(shop);
			}
		}
		int shopNum = shopList.size();
		int pageNum = (int) Math.ceil(1.0 * shopNum
				/ MmochatConstant.shopNumShowPerPage);

		int curPage = page;
		curPage = Math.min(curPage, pageNum - 1);
		curPage = Math.max(curPage, 0);
		int showShopNum = Math.min(shopNum - curPage
				* MmochatConstant.shopNumShowPerPage,
				MmochatConstant.shopNumShowPerPage);
		int baseIndex = curPage * MmochatConstant.shopNumShowPerPage;

		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("菜单");
		tlv.setRightName("返回");
		tlv.setTitle("二手" + type.toString());
		tlv.setWinHeight(208);
		tlv.setWinId(MmochatConstant.TLV_WIN_PLAYERSHOP_SELL);
		tlv
				.addRightTLV(new CloseWinTLV(
						MmochatConstant.TLV_WIN_PLAYERSHOP_SELL));

		for (int i = 0; i < showShopNum; i++) {
			MmochatPlayerShop shop = null;
			try {
				shop = shopList.get(i + baseIndex);
			} catch (Exception e) {
			}
			if (shop != null
					&& shop.getState() == MmochatPlayerShopState.二手店出售中) {
				String item = shop.getName();
				item += MmochatUtil.wrapColor("\n售价:"
						+ MmochatUtil.getColorMoney(shop.getSell_price())
						+ "文钱", Color.green);
				item += MmochatUtil.wrapColor("\n信用:"
						+ MmochatShopService.getShopStar(shop) + "\n柜台数:"
						+ shop.getPageNum() + "个", Color.yellow);

				tlv.addItemEvent(item, new ClearDataTLV(), new AddDataTLV(
						(int) shop.getId()), new AddDataTLV((int) shop
						.getVersion()));
			}
		}

		CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
		msgbox.setContent(new Text("您确定要购买此商店么?"));
		msgbox.setLeftName("确定");
		msgbox.setRightName("取消");
		msgbox.addLeftTLV(new AddDataTLV((int) type.getKey()));
		msgbox.addLeftTLV(new AddDataTLV((int) page));
		msgbox.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_21, (int) roleId));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		msgbox.addLeftTLV(wait);

		ShowPopupMenuTLV menu = new ShowPopupMenuTLV();
		menu.addItemEvent("购买", msgbox);

		menu.addItemEvent("下一页", new ClearDataTLV(), new AddDataTLV((int) type
				.getKey()), new AddDataTLV((int) page + 1), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_20, (int) roleId),
				new ShowWaitingMsgBoxTLV());

		menu.addItemEvent("上一页", new ClearDataTLV(), new AddDataTLV((int) type
				.getKey()), new AddDataTLV((int) page - 1), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_20, (int) roleId),
				new ShowWaitingMsgBoxTLV());

		tlv.addLeftTLV(menu);
		return tlv;
	}

	// 查看指定页，指定类型的二手商店列表
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_20(SkymobiHandler handler,
			Mmochat_19_20C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		MmochatPlayerShopType type = MmochatPlayerShopType.get(req
				.getShopTypeValue());
		if (type == null) {
			return MmochatUtil.msgbox(req.getHallState(), "没有此类型的商店!");
		}
		CreateTlvWin tlv = getSellShopTlv(type, req.getPage(), req.getRoleId());

		if (tlv == null) {
			return MmochatUtil.msgbox(req.getHallState(), "暂时还没有此类商店出售!");
		}
		return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_SELL), tlv);
	}

	// 购买二手商店
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_21(SkymobiHandler handler,
			Mmochat_19_21C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		if (me.getLevel() < MmochatConstant.levelNeedToCreateShop) {
			return MmochatUtil.msgbox(req.getHallState(), "人物达到"
					+ MmochatConstant.levelNeedToCreateShop + "级才能购买二手商店!");
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (shop.getState() != MmochatPlayerShopState.二手店出售中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店已经取消出售，请刷新!");
		}
		if (shop.getVersion() != req.getVersion()) {
			return MmochatUtil.msgbox(req.getHallState(),
					"此商店的出售信息已经修改，请刷新后再买!");
		}

		// 金钱判断
		if (me.getSmall_money() < shop.getSell_price()) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店售价"
					+ MmochatUtil.getColorMoney(shop.getSell_price())
					+ "文,您的金钱不够!");
		}

		// 已有商店数判断
		int curShopNum = 0;
		for (MmochatPlayerShop shop1 : MmochatShopService.shopMap.values()) {
			if (shop1.getRoleId() == me.getRoleId()
					&& shop1.getState() != MmochatPlayerShopState.倒闭) {
				curShopNum++;
				if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
					break;
				}
			}
		}
		if (curShopNum >= MmochatConstant.maxShopNumPerRole) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经拥有"
					+ MmochatConstant.maxShopNumPerRole + "家商店了，已经够您忙的了!");
		}

		int ownerRoleId;
		int sellPrice;
		synchronized (shop) {
			if (shop.getState() != MmochatPlayerShopState.二手店出售中) {
				return MmochatUtil.msgbox(req.getHallState(), "此商店已经取消出售，请刷新!");
			}
			if (shop.getVersion() != req.getVersion()) {
				return MmochatUtil.msgbox(req.getHallState(),
						"此商店的出售信息已经修改，请刷新后再买!");
			}
			ownerRoleId = shop.getRoleId();
			sellPrice = shop.getSell_price();
			try {
				MmochatDao.buyShop(me.getRoleId(), MmochatPlayerShopState.未营业
						.getKey(), me.getName(), shop.getId(), shop
						.getSell_price(), shop.getRoleId());
			} catch (Exception e) {
				e.printStackTrace();
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			me.addAndUpdateSmall_money(-1 * sellPrice);
			shop.setState(MmochatPlayerShopState.未营业);
			shop.setRoleId(me.getRoleId());
			shop.setRoleName(me.getName());
			shop.getSellLog().clear();
			shop.addVersion();
		}
		MmochatPlayer he = MmochatMainService.getRoleFromBuffer(ownerRoleId);
		if (he != null) {
			he.addAndUpdateSmall_money(sellPrice);
		}
		String content = "您的商店<" + shop.getName() + ">已经成功售出，"
				+ MmochatUtil.getColorMoney(sellPrice) + "文钱已经打入您的包裹。";
		MmochatGmService.sendKufuPrivateMsg(ownerRoleId, content, false);

		CreateTlvWin tlv = getSellShopTlv(MmochatPlayerShopType.get(req
				.getType()), req.getPage(), req.getRoleId());

		if (tlv == null) {
			return MmochatUtil.msgbox(req.getHallState(), "暂时还没有此类商店出售!");
		}
		CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
		msgbox.setContent(new Text("您花费了"
				+ MmochatUtil.getColorMoney(sellPrice)
				+ "文钱购买了一间二手商店，请到<我的商店>进行管理!"));
		msgbox.setLeftName("确定");
		return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_SELL), tlv, msgbox);
	}

	// 购买商店物品
	@SuppressWarnings("unchecked")
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_22(SkymobiHandler handler,
			Mmochat_19_22C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (me.isGuest()) {
			return MmochatUtil.msgbox(req.getHallState(), "游客不能购买商店物品!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		if (MmochatNpcService.checkStorePwd(me)) {
			return null;
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (me.getRoleId() == shop.getRoleId()) {
			return MmochatUtil.msgbox(req.getHallState(), "不能购买自己商店里的商品!");
		}
		if (shop.getState() != MmochatPlayerShopState.营业中) {
			Mmochat_19_2S pack = new Mmochat_19_2S();
			pack.setHallState(req.getHallState());
			pack.setShop(shop);
			pack.setCurPage(req.getCurPage());
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "此商店已经停止营业!");
		}
		if (shop.getVersion() != req.getVersion()) {
			Mmochat_19_2S pack = new Mmochat_19_2S();
			pack.setHallState(req.getHallState());
			pack.setShop(shop);
			pack.setCurPage(req.getCurPage());
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(),
					"购买失败，此商店的物品出售信息已经修改，请重新查看再购买!");
		}
		int num = req.getNum();
		if (num <= 0) {
			return MmochatUtil.msgbox(req.getHallState(), "购买数量不能为0!");
		}
		MmochatPackageGrid grid = shop.getGrids().get(req.getGridId());
		if (grid == null || !grid.isCanUse()) {
			Mmochat_19_2S pack = new Mmochat_19_2S();
			pack.setHallState(req.getHallState());
			pack.setShop(shop);
			pack.setCurPage(req.getCurPage());
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
		}
		MmochatCommonObject obj = grid.getObject();
		if (obj == null || obj.getOverlapNum() == 0) {
			Mmochat_19_2S pack = new Mmochat_19_2S();
			pack.setHallState(req.getHallState());
			pack.setShop(shop);
			pack.setCurPage(req.getCurPage());
			MmochatUtil.sendCommonPack(me, pack);
			return MmochatUtil.msgbox(req.getHallState(), "此物品已经卖光!");
		}
		int buyNum = Math.min(num, obj.getOverlapNum());
		if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
			if (me.isPetFull()) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您携带的宠物数量已满，请先存放到仓库再行购买!");
			}
		} else {
			if (me.calRestPackageSpace(obj.getType(), obj.isCanOverlap(), obj
					.isCanDeal()) < buyNum) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您的包裹空间不足，请先清理包裹!");
			}
		}
		long price = buyNum * obj.getSellPrice();
		if (price > Integer.MAX_VALUE) {
			return MmochatUtil.msgbox(req.getHallState(), "您包裹中的金钱不足!");
		}
		if (price > me.getSmall_money()) {
			return MmochatUtil.msgbox(req.getHallState(), "您包裹中的金钱不足!");
		}
		double tax = getShopTax(shop);

		// 备份我的包裹信息
		Map<Integer, MmochatPackageGrid> packageBak = null;
		if (shop.getShopType() != MmochatPlayerShopType.宠物店) {
			packageBak = (Map<Integer, MmochatPackageGrid>) MmochatUtil
					.deepClone(me.getMyPackage(),
							new TypeToken<Map<Integer, MmochatPackageGrid>>() {
							}.getType());
		}

		MmochatPet pet = null;
		int costMoney = 0;
		synchronized (shop) {
			if (shop.getVersion() != req.getVersion()) {
				Mmochat_19_2S pack = new Mmochat_19_2S();
				pack.setHallState(req.getHallState());
				pack.setShop(shop);
				pack.setCurPage(req.getCurPage());
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil.msgbox(req.getHallState(),
						"购买失败，此商店的物品出售信息已经修改，请重新查看再购买!");
			}
			grid = shop.getGrids().get(req.getGridId());
			if (grid == null || !grid.isCanUse()) {
				Mmochat_19_2S pack = new Mmochat_19_2S();
				pack.setHallState(req.getHallState());
				pack.setShop(shop);
				pack.setCurPage(req.getCurPage());
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil.msgbox(req.getHallState(), "此物品不存在!");
			}
			obj = grid.getObject();
			if (obj == null || obj.getOverlapNum() == 0) {
				Mmochat_19_2S pack = new Mmochat_19_2S();
				pack.setHallState(req.getHallState());
				pack.setShop(shop);
				pack.setCurPage(req.getCurPage());
				MmochatUtil.sendCommonPack(me, pack);
				return MmochatUtil.msgbox(req.getHallState(), "此物品已经卖光!");
			}
			buyNum = Math.min(num, obj.getOverlapNum());
			if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
				if (me.isPetFull()) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您携带的宠物数量已满，请先存放到仓库再行购买!");
				}
			} else {
				if (me.calRestPackageSpace(obj.getType(), obj.isCanOverlap(),
						obj.isCanDeal()) < buyNum) {
					return MmochatUtil.msgbox(req.getHallState(),
							"您的包裹空间不足，请先清理包裹!");
				}
			}
			price = buyNum * obj.getSellPrice();
			if (price > Integer.MAX_VALUE) {
				return MmochatUtil.msgbox(req.getHallState(), "您包裹中的金钱不足!");
			}
			if (price > me.getSmall_money()) {
				return MmochatUtil.msgbox(req.getHallState(), "您包裹中的金钱不足!");
			}

			int oldNum = obj.getOverlapNum();
			costMoney = (int) price;
			// 税率计算
			int earnMoney = (int) (costMoney * (1 - tax));

			if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
				pet = obj.getPet();
				if (pet == null) {
					return MmochatUtil.msgbox(req.getHallState(), "此宠物不存在!");
				}
				grid.setObject(null);
				me.addPet(pet);
				me.addAndUpdateSmall_money(-1 * costMoney);
				shop.addShopMoney(earnMoney);
			} else {
				if (buyNum >= obj.getOverlapNum()) {
					// 全部购买
					grid.setObject(null);
					me.addObjectToPackage(obj);
					me.addAndUpdateSmall_money(-1 * costMoney);
					shop.addShopMoney(earnMoney);
				} else {
					// 部分购买
					MmochatCommonObject newObj = obj.getCloneWithNewId();
					newObj.setOverlapNum(buyNum);
					obj.delOverlapNum(buyNum);
					me.addObjectToPackage(newObj);
					me.addAndUpdateSmall_money(-1 * costMoney);
					shop.addShopMoney(earnMoney);
				}
			}

			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd kk:mm:ss");
			String now = sdf.format(new Date());
			String sellLog = obj.getName();
			String detail = "\n数量:" + buyNum + "个";
			detail += "\n售价:" + MmochatUtil.getColorMoney(costMoney) + "文";
			detail += "\n利润:" + MmochatUtil.getColorMoney(earnMoney) + "文";
			detail += "\n买家ID:" + Integer.toHexString(me.getRoleId() * 3);
			detail += "\n时间:" + now;
			sellLog += MmochatUtil.wrapColor(detail, Color.yellow);

			shop.addSellLog(sellLog);
			int winScore = costMoney;
			// 保存商店格子、商店金钱、我的包裹、我的金钱、交易记录
			try {
				if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
					MmochatDao.buyShopPet(me, shop, costMoney, earnMoney,
							winScore);
				} else {
					MmochatDao.buyShopObj(me, shop, costMoney, earnMoney,
							winScore);
				}
			} catch (Exception e) {
				e.printStackTrace();
				if (shop.getShopType() == MmochatPlayerShopType.宠物店) {
					if (pet != null) {
						me.delPet(pet.getId());
						grid.setObject(obj);
					}
				} else {
					me.setMyPackage(packageBak);
					obj.setOverlapNum(oldNum);
					grid.setObject(obj);
				}
				me.addAndUpdateSmall_money(costMoney);
				shop.addShopMoney(-1 * earnMoney);
				shop.getSellLog().remove(sellLog);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙，请稍候再试!");
			}
			shop.addScore(winScore);
			shop.addVersion();
		}
		Mmochat_19_2S pack = new Mmochat_19_2S();
		pack.setHallState(req.getHallState());
		pack.setShop(shop);
		pack.setCurPage(req.getCurPage());
		MmochatUtil.sendCommonPack(me, pack);
		if (pet != null) {
			Mmochat_5_10S petPack = new Mmochat_5_10S();
			petPack.addPet(pet);
			MmochatUtil.sendCommonPack(me, petPack);
		}

		try {
			// 购买记录
			Map<Integer, Long> buyLogs = buyLog.get(me.getRoleId());
			if (buyLogs == null) {
				buyLogs = new ConcurrentHashMap<Integer, Long>();
				buyLog.put(me.getRoleId(), buyLogs);
			}
			Long cost = buyLogs.get(req.getShopId());
			if (cost == null) {
				cost = 0L;
			}
			cost += costMoney;
			buyLogs.put(req.getShopId(), cost);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return MmochatUtil.msgbox(req.getHallState(), "购买成功!您花费了"
				+ MmochatUtil.getColorMoney(price) + "文钱购买了"
				+ MmochatUtil.wrapColor(buyNum + "", Color.green) + "个"
				+ MmochatUtil.wrapColor(obj.getName(), Color.yellow) + "!");
	}

	// 获取出售记录tlv
	private CreateTlvWin getSellLogTlv(MmochatPlayerShop shop, int roleId,
			int page) {
		if (shop == null) {
			return null;
		}
		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("菜单");
		tlv.setRightName("返回");
		tlv.setTitle("出售记录");
		tlv.setWinHeight(240);
		tlv.setWinId(MmochatConstant.TLV_WIN_PLAYERSHOP_SELLLOG);
		tlv.addRightTLV(new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_SELLLOG));

		List<String> logList = new ArrayList<String>(shop.getSellLog());
		Collections.reverse(logList);

		int pageSize = 10;
		int pageNum = (logList.size() + pageSize - 1) / pageSize;
		page = Math.min(pageNum - 1, page);
		page = Math.max(0, page);
		int base = page * pageSize;
		int showNum = Math.min(pageSize, logList.size() - base);

		for (int i = 0; i < showNum; i++) {
			String log = null;
			try {
				log = logList.get(base + i);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (log != null) {
				tlv.addItemEvent((base + i + 1) + "、" + log);
			}
		}
		ShowPopupMenuTLV menu = new ShowPopupMenuTLV();

		menu.addItemEvent("下一页", new ClearDataTLV(), new AddDataTLV((int) shop
				.getId()), new AddDataTLV((int) roleId), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_23, (int) (page + 1)),
				new ShowWaitingMsgBoxTLV());

		menu.addItemEvent("上一页", new ClearDataTLV(), new AddDataTLV((int) shop
				.getId()), new AddDataTLV((int) roleId), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_23, (int) (page - 1)),
				new ShowWaitingMsgBoxTLV());

		menu.addItemEvent("第一页", new ClearDataTLV(), new AddDataTLV((int) shop
				.getId()), new AddDataTLV((int) roleId), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_23, (int) 0),
				new ShowWaitingMsgBoxTLV());

		menu.addItemEvent("最后页", new ClearDataTLV(), new AddDataTLV((int) shop
				.getId()), new AddDataTLV((int) roleId), new SendDataTLV(
				MmochatConstant.getClientModuleId(), MmochatMain
						.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_23, (int) 11111),
				new ShowWaitingMsgBoxTLV());

		tlv.addLeftTLV(menu);
		return tlv;
	}

	// 查看出售记录
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_23(SkymobiHandler handler,
			Mmochat_19_23C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatPlayerShop shop = shopMap.get(req.getShopId());
		if (shop == null || shop.getState() == MmochatPlayerShopState.倒闭) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店不存在或已经倒闭!");
		}
		if (!shop.isShopOwner(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经不是此店掌柜!");
		}
		CreateTlvWin tlv = getSellLogTlv(shop, me.getRoleId(), req.getPage());
		if (tlv == null) {
			return MmochatUtil.msgbox(req.getHallState(), "没有出售记录!");
		}
		return MmochatUtil.tlvResponse(req.getHallState(), new CloseWinTLV(
				MmochatConstant.TLV_WIN_PLAYERSHOP_SELLLOG), tlv);
	}

	// 查看指定ID、指定柜台的商店信息
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_25(SkymobiHandler handler,
			Mmochat_19_25C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}

		Integer shopId = null;
		try {
			shopId = Integer.parseInt(req.getShopIdStr());
		} catch (NumberFormatException e) {
			return MmochatUtil.msgbox(req.getHallState(), "只能输入数字!");
		}

		MmochatPlayerShop shop = shopMap.get(shopId);
		if (shop == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店已倒闭或不存在!");
		}
		if (shop.getState() != MmochatPlayerShopState.营业中) {
			return MmochatUtil.msgbox(req.getHallState(), "此商店目前暂未营业!");
		}
		Mmochat_19_2S pack = new Mmochat_19_2S();
		pack.setHallState(req.getHallState());
		pack.setShop(shop);
		pack.setCurPage(0);
		return pack;
	}

	// 根据指定页、指定物品名字查询商店
	@SkymobiService
	public SkymobiProtocolMessage handleC_19_26(SkymobiHandler handler,
			Mmochat_19_26C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!MmochatConstant.isPlayerShopOpen) {
			return MmochatUtil.msgbox(req.getHallState(), "玩家商会临时维护中!");
		}
		MmochatObjectType objType = MmochatObjectType
				.get(req.getObjTypeValue());
		if (objType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "暂不支持此类别物品查询!");
		}
		String objName = req.getObjName();
		if (objName == null) {
			return MmochatUtil.msgbox(req.getHallState(), "请输入物品名!");
		}
		// 判断名字是否输入正确
		boolean isNameOk = false;
		List<MmochatEquipAttributeType> attrTypeList = new ArrayList<MmochatEquipAttributeType>();
		Integer petLevel = null;
		List<MmochatPlayerShopType> shopTypeList = new ArrayList<MmochatPlayerShopType>();
		try {
			switch (objType) {
			case 道具:
				if (MmochatPropType.valueOf(objName) != null) {
					isNameOk = true;
					shopTypeList.add(MmochatPlayerShopType.道具店);
					shopTypeList.add(MmochatPlayerShopType.杂货店);
					shopTypeList.add(MmochatPlayerShopType.材料店);
					shopTypeList.add(MmochatPlayerShopType.装备店);
				}
				break;
			case 材料:
				if (MmochatMaterialType.valueOf(objName) != null) {
					isNameOk = true;
					shopTypeList.add(MmochatPlayerShopType.材料店);
					shopTypeList.add(MmochatPlayerShopType.杂货店);
					shopTypeList.add(MmochatPlayerShopType.道具店);
					shopTypeList.add(MmochatPlayerShopType.装备店);
				}
				break;
			case 装备: {
				// 装备名
				// 装备名#属性#属性
				int hit = objName.indexOf('#');
				if (hit != -1) {
					String tail = objName.substring(hit + 1);
					objName = objName.substring(0, hit);
					hit = tail.indexOf('#');
					while (hit != -1) {
						String attr = tail.substring(0, hit);
						tail = tail.substring(hit + 1);
						hit = tail.indexOf('#');
						boolean attrOk = false;
						for (MmochatEquipAttributeType type : MmochatEquipAttributeType
								.values()) {
							if (type.getStartInfo().equals(attr)) {
								attrTypeList.add(type);
								attrOk = true;
								break;
							}
						}
						if (!attrOk) {
							return MmochatUtil.msgbox(req.getHallState(),
									"属性类型输入不正确!");
						}
					}
					if (tail != null) {
						boolean attrOk = false;
						for (MmochatEquipAttributeType type : MmochatEquipAttributeType
								.values()) {
							if (type.getStartInfo().equals(tail)) {
								attrTypeList.add(type);
								attrOk = true;
								break;
							}
						}
						if (!attrOk) {
							return MmochatUtil.msgbox(req.getHallState(),
									"属性类型输入不正确!");
						}
					}
				}
				isNameOk = MmochatEquipService.isEquipName(objName);
				shopTypeList.add(MmochatPlayerShopType.装备店);
				shopTypeList.add(MmochatPlayerShopType.杂货店);
				shopTypeList.add(MmochatPlayerShopType.道具店);
				shopTypeList.add(MmochatPlayerShopType.材料店);
				break;
			}
			case 宠物: {
				int hit = objName.indexOf('#');
				if (hit != -1) {
					String tail = objName.substring(hit + 1);
					objName = objName.substring(0, hit);
					if (tail != null) {
						try {
							petLevel = Integer.parseInt(tail);
						} catch (Exception e) {
							return MmochatUtil.msgbox(req.getHallState(),
									"宠物等级只能是数字，格式如：树妖#120，表示查找110-120级的树妖。");
						}
					}
				}
				if (MmochatRoleType.valueOf(objName) != null) {
					isNameOk = true;
					shopTypeList.add(MmochatPlayerShopType.宠物店);
				}
				break;
			}
			case 药品:
				if (MmochatDrugType.valueOf(objName) != null) {
					isNameOk = true;
					shopTypeList.add(MmochatPlayerShopType.材料店);
					shopTypeList.add(MmochatPlayerShopType.杂货店);
					shopTypeList.add(MmochatPlayerShopType.道具店);
					shopTypeList.add(MmochatPlayerShopType.装备店);
				}
				break;
			default:
				return MmochatUtil.msgbox(req.getHallState(), "暂不支持此类别物品查询!");
			}
		} catch (IllegalArgumentException e) {
			// 名字输入不正确
		}
		if (!isNameOk) {
			return MmochatUtil.msgbox(req.getHallState(), objType.toString()
					+ "名字输入不正确!");
		}

		List<MmochatPlayerShop> shopList = new ArrayList<MmochatPlayerShop>();
		for (MmochatPlayerShopType type : shopTypeList) {
			ConcurrentLinkedQueue<MmochatPlayerShop> shopQueue = shops
					.get(type);
			if (shopQueue == null) {
				continue;
			}
			MmochatPlayerShop[] shopArray = shopQueue
					.toArray(new MmochatPlayerShop[0]);
			for (MmochatPlayerShop shop : shopArray) {
				try {
					if (shop != null
							&& shop.getState() == MmochatPlayerShopState.营业中) {
						// 判断是否有指定物品
						if (objType == MmochatObjectType.宠物) {
							for (MmochatPackageGrid grid : shop.getGrids()
									.values()) {
								if (grid.isCanUse()) {
									MmochatCommonObject obj = grid.getObject();
									if (obj != null && obj.getPet() != null) {
										if (obj.getPet().getRoleType()
												.toString().equals(objName)) {
											if (petLevel == null) {
												shopList.add(shop);
												break;
											} else {
												if (obj.getPet().getLevel() >= petLevel - 10
														&& obj.getPet()
																.getLevel() <= petLevel) {
													shopList.add(shop);
													break;
												}
											}
										}
									}
								}
							}
						} else {
							for (MmochatPackageGrid grid : shop.getGrids()
									.values()) {
								if (grid.isCanUse()) {
									MmochatCommonObject obj = grid.getObject();
									if (obj != null) {
										if (obj.getName().equals(objName)) {
											if (attrTypeList.size() == 0) {
												shopList.add(shop);
												break;
											} else {
												boolean add = true;
												for (MmochatEquipAttributeType attrType : attrTypeList) {
													if (obj.getEquipPinkAttr() != null
															&& obj
																	.getEquipPinkAttr()
																	.getType() == attrType) {
														continue;
													}
													if (obj
															.getEquipGreenLightAttr() != null
															&& obj
																	.getEquipGreenLightAttr()
																	.getType() == attrType) {
														continue;
													}
													boolean hasAttr = false;
													for (MmochatEquipAttribute attr : obj
															.getEquipRandAttr()) {
														if (attr.getType() == attrType) {
															hasAttr = true;
															break;
														}
													}
													if (!hasAttr) {
														add = false;
														break;
													}
												}
												if (add) {
													shopList.add(shop);
													break;
												}
											}
										}
									}
								}
							}
						}

					}
				} catch (Exception e) {
				}
			}
		}
		if (shopList.size() == 0) {
			return MmochatUtil.msgbox(req.getHallState(), "没有找到出售此商品的店!");
		}

		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setLeftName("进入");
		tlv.setRightName("返回");
		tlv.setTitle(objName);
		tlv.setWinHeight(208);
		tlv.addRightTLV(new CloseWinTLV());

		int index = 1;
		for (MmochatPlayerShop shop : shopList) {
			tlv.addItemEvent(shop.getName(), new ClearDataTLV(),
					new AddDataTLV((int) shop.getId()));
			index++;
			if (index >= 40) {
				break;
			}
		}

		tlv.addLeftTLV(new AddDataTLV((int) 0));
		tlv.addLeftTLV(new SendDataTLV(MmochatConstant.getClientModuleId(),
				MmochatMain.getModuleInstID(),
				MmochatConstant.MSGCODE_MMOCHAT_C_19_2, (int) me.getRoleId()));
		tlv.addLeftTLV(new ShowWaitingMsgBoxTLV());

		return MmochatUtil.tlvResponse(req.getHallState(), tlv);
	}

	// 计算商店星级
	public static String getShopStar(MmochatPlayerShop shop) {
		if (shop == null) {
			return "";
		}
		long score = shop.getScore();
		for (int i = 0; i < shopLevelScore.length; i++) {
			if (score < shopLevelScore[i]) {
				return shopStarString[i];
			}
		}
		return shopStarString[shopStarString.length - 1];
	}

	// 计算商店税率(0-1)
	public static double getShopTax(MmochatPlayerShop shop) {
		if (shop == null) {
			return 0.1;
		}
		Double baseTax = null;
		long score = shop.getScore();
		for (int i = 0; i < shopLevelScore.length; i++) {
			if (score < shopLevelScore[i]) {
				baseTax = shopStarBaseTax[i];
				break;
			}
		}
		if (baseTax == null) {
			baseTax = shopStarBaseTax[shopStarBaseTax.length - 1];
		}
		int addShopPage = shop.getPageNum() - 1;
		return baseTax - addShopPage * 0.002;
	}

	// 计算商店每日积分消耗
	public static int getShopDailyScoreCost(MmochatPlayerShop shop) {
		if (shop == null) {
			return 0;
		}
		Integer baseCost = null;
		long score = shop.getScore();
		for (int i = 0; i < shopLevelScore.length; i++) {
			if (score < shopLevelScore[i]) {
				baseCost = shopLevelDayCost[i];
				break;
			}
		}
		if (baseCost == null) {
			baseCost = shopLevelDayCost[shopLevelDayCost.length - 1];
		}
		return baseCost;
	}

	// 计算商店每日消耗
	public static int getShopDailyCostMoney(MmochatPlayerShop shop) {
		if (shop == null) {
			return 20000;
		}
		return shop.getPageNum() * MmochatConstant.dailyMoneyCostPerShopPage;
	}
}
